{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a target=\"_blank\" href=\"https://colab.research.google.com/github/cohere-ai/notebooks/blob/main/notebooks/llmu/Parameters_for_Controlling_Outputs.ipynb\">\n",
    "  <img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/>\n",
    "</a>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Parameters for Controlling Outputs\n",
    "\n",
    "In this notebook, you’ll learn about the parameters you can use to control the Chat endpoint's outputs.\n",
    "\n",
    "*Read the accompanying [blog post here](https://docs.cohere.com/docs/parameters-for-controlling-outputs).*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Overview"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The notebook has 3 sections:\n",
    "- **Model Type** - Select a variation of the Command model.\n",
    "- **Randomness** - Use the `temperature` parameter to control the level of randomness of the model.\n",
    "- **Conciseness** - Set the `preamble` parameter to an empty string to make model responses more concise."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Setup"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We'll start by installing the tools we'll need and then importing them."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "z49_e7Ma2IgU",
    "outputId": "877c1228-c424-433f-d22c-1565c8445d35"
   },
   "outputs": [],
   "source": [
    "! pip install cohere -q"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#@title Enable text wrapping in Google Colab\n",
    "\n",
    "from IPython.display import HTML, display\n",
    "\n",
    "def set_css():\n",
    "  display(HTML('''\n",
    "  <style>\n",
    "    pre {\n",
    "        white-space: pre-wrap;\n",
    "    }\n",
    "  </style>\n",
    "  '''))\n",
    "get_ipython().events.register('pre_run_cell', set_css)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Fill in your Cohere API key in the next cell. To do this, begin by [signing up to Cohere](https://os.cohere.ai/) (for free!) if you haven't yet. Then get your API key [here](https://dashboard.cohere.com/api-keys)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cohere\n",
    "\n",
    "# Paste your API key here. Remember to not share publicly\n",
    "co = cohere.Client(\"COHERE_API_KEY\") # Insert your Cohere API key"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Model Type\n",
    "\n",
    "When calling the Chat endpoint, use the `model` parameter to choose from several variations of the Command model. \n",
    "\n",
    "[See the documentation](https://docs.cohere.com/docs/models#command) for the most updated list of available Cohere models."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "  <style>\n",
       "    pre {\n",
       "        white-space: pre-wrap;\n",
       "    }\n",
       "  </style>\n",
       "  "
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hi! Hello there! How's it going? I hope you're having a fantastic day so far. Is there anything I can help you with?\n"
     ]
    }
   ],
   "source": [
    "response = co.chat(message=\"Hello\",\n",
    "                   model=\"command-r\")\n",
    "print(response.text)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "Df4lLD-P3aj_"
   },
   "source": [
    "## Randomness\n",
    "\n",
    "You can use the `temperature` parameter to control the level of randomness of the model. It is a value between 0 and 1. As you increase the temperature, the model gets more creative and random. Temperature can be tuned for different problems, and most people will find that the default temperature of 0.3 is a good starting point.\n",
    "\n",
    "Consider the example below, where we ask the model to generate alternative titles for a blog post. Prompting the endpoint five times when the temperature is set to 0 yields the same output each time. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "  <style>\n",
       "    pre {\n",
       "        white-space: pre-wrap;\n",
       "    }\n",
       "  </style>\n",
       "  "
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\"Unleashing the Power of Generation: A Guide to the Exciting World of Retrieval-Augmented Creation\"\n",
      "\"Unleashing the Power of Generation: A Guide to the Exciting World of Retrieval-Augmented Creation\"\n",
      "\"Unleashing the Power of Generation: A Guide to the Exciting World of Retrieval-Augmented Creation\"\n",
      "\"Unleashing the Power of Generation: A Guide to the Exciting World of Retrieval-Augmented Creation\"\n",
      "\"Unleashing the Power of Generation: A Guide to the Exciting World of Retrieval-Augmented Creation\"\n"
     ]
    }
   ],
   "source": [
    "message = \"\"\"Suggest a more exciting title for a blog post titled: Intro to Retrieval-Augmented Generation. \\\n",
    "Respond in a single line.\"\"\"\n",
    "\n",
    "for _ in range(5):\n",
    "    response = co.chat(message=message,\n",
    "                       temperature=0)\n",
    "    print(response.text)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "However, if we increase the temperature to the maximum value of 1, the model gives different proposals."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "  <style>\n",
       "    pre {\n",
       "        white-space: pre-wrap;\n",
       "    }\n",
       "  </style>\n",
       "  "
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\"Unleashing the Power of Generation: A Guide to the Future of Retrieval-Augmented Creation\"\n",
      "\"Unleashing the Power of Generation: A Guide to the Exciting World of Retrieval-Augmented Creation\"\n",
      "\"Unleashing the Power of RAG: A Guide to the Future of Generation\"\n",
      "\"Unleashing the Power of Augmented Generation: A Guide to the Future of AI Text Generation\"\n",
      "\"Unleashing the Power of Generation: A Guide to the Exciting World of Retrieval-Augmented Creation\"\n"
     ]
    }
   ],
   "source": [
    "message = \"\"\"Suggest a more exciting title for a blog post titled: Intro to Retrieval-Augmented Generation. \\\n",
    "Respond in a single line.\"\"\"\n",
    "\n",
    "for _ in range(5):\n",
    "    response = co.chat(message=message,\n",
    "                       temperature=1)\n",
    "    print(response.text)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Conciseness\n",
    "\n",
    "Sometimes, the model provides more context than you need to address a query.  For instance, consider what happens when we ask the model a simple question: \"How many eggs are in one dozen?\"."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "  <style>\n",
       "    pre {\n",
       "        white-space: pre-wrap;\n",
       "    }\n",
       "  </style>\n",
       "  "
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "There are 12 eggs in one dozen. The term \"dozen\" is used to represent the number 12, and it's commonly used when referring to measurements or quantities, especially for eggs. So, when you buy or hear about a dozen eggs, it means you're dealing with 12 eggs.\n"
     ]
    }
   ],
   "source": [
    "response = co.chat(message=\"How many eggs are in one dozen?\")\n",
    "print(response.text)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The model answers the question, but it uses multiple sentences when the first sentence would have been sufficient.\n",
    "\n",
    "We can get the model to shorten its response by setting the `preamble` parameter to an empty string."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "  <style>\n",
       "    pre {\n",
       "        white-space: pre-wrap;\n",
       "    }\n",
       "  </style>\n",
       "  "
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "There are 12 eggs in one dozen.\n"
     ]
    }
   ],
   "source": [
    "response = co.chat(message=\"How many eggs are in one dozen?\",\n",
    "                   preamble=\"\")\n",
    "print(response.text)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As you have explored in this notebook, the Chat endpoint is a versatile tool that empowers developers with a useful array of options and parameters."
   ]
  }
 ],
 "metadata": {
  "colab": {
   "provenance": [],
   "toc_visible": true
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
